<!DOCTYPE html>
<html lang="zh-CN" data-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备管理 - Waud 监控系统</title>
    <script src="/static/js/jquery.min.js"></script>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root {
            --bg-color: #1a1a1a;
            --card-bg: #2d2d2d;
            --text-color: #e0e0e0;
            --border-color: #444;
            --header-bg: #252525;
            --hover-bg: rgba(255, 255, 255, 0.05);
        }

        [data-theme="light"] {
            --bg-color: #f5f5f5;
            --card-bg: #ffffff;
            --text-color: #333333;
            --border-color: #ddd;
            --header-bg: #f8f9fa;
            --hover-bg: rgba(0, 0, 0, 0.05);
        }

        body {
            background-color: var(--bg-color);
            color: var(--text-color);
            padding-top: 20px;
            transition: background-color 0.3s, color 0.3s;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .card {
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            margin-bottom: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            overflow: hidden;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 24px rgba(0,0,0,0.15);
        }

        .card-header {
            background-color: var(--header-bg);
            border-bottom: 1px solid var(--border-color);
            font-weight: 600;
            padding: 15px 20px;
        }

        .theme-toggle-btn {
            border-radius: 50px;
            padding: 8px 16px;
            margin-bottom: 20px;
        }

        .refresh-btn {
            margin-left: 10px;
            border-radius: 6px;
        }

        h1, h2, h3, h4, h5, h6 {
            color: var(--text-color);
        }

        .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            border-radius: 6px;
        }

        .btn-success {
            background-color: #28a745;
            border-color: #28a745;
            border-radius: 6px;
        }

        .btn-warning {
            background-color: #ffc107;
            border-color: #ffc107;
            border-radius: 6px;
        }

        .btn-secondary {
            background-color: #6c757d;
            border-color: #6c757d;
            border-radius: 6px;
        }

        .table {
            color: var(--text-color);
            background-color: var(--card-bg);
        }

        .table th {
            border-top: 1px solid var(--border-color);
            border-bottom: 1px solid var(--border-color);
        }

        .table td {
            border-top: 1px solid var(--border-color);
        }

        .table-hover tbody tr:hover {
            background-color: var(--hover-bg);
        }

        .text-success {
            color: #28a745 !important;
        }

        .text-danger {
            color: #dc3545 !important;
        }

        .navbar-brand {
            color: var(--text-color) !important;
        }

        /* 设备卡片网格布局 */
        .devices-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .device-card {
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .device-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 24px rgba(0,0,0,0.2);
        }

        .device-card-header {
            font-weight: 600;
            margin-bottom: 10px;
            color: #007bff;
        }

        .device-card-body {
            padding: 15px;
        }

        .device-status {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 500;
        }

        .status-online {
            background-color: rgba(40, 167, 69, 0.2);
            color: #28a745;
        }

        .status-offline {
            background-color: rgba(220, 53, 69, 0.2);
            color: #dc3545;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container-fluid {
                padding: 0 10px;
            }
            
            .card {
                margin-bottom: 15px;
            }
            
            .devices-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 576px) {
            .screenshot-img {
                height: 120px;
            }
        }

        .loading-spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .spinner-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 200px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="d-flex justify-content-between align-items-center">
            <h1 class="mb-4">设备管理</h1>
            <a href="/" class="btn btn-secondary" style="margin-right: 10px;">返回首页</a>
            <!-- 主题切换按钮 -->
            <button id="themeToggle" class="btn btn-secondary theme-toggle-btn">
                <span class="theme-icon">🌙</span> 暗黑主题
            </button>
        </div>
        
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <span>设备列表</span>
                        <button id="refreshDevices" class="btn btn-sm btn-primary refresh-btn">
                            刷新
                        </button>
                    </div>
                    <div class="card-body">
                        <div class="devices-grid" id="devicesGrid">
                            <div class="text-center">
                                <div class="spinner-container">
                                    <div class="loading-spinner"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 主题切换功能
        function toggleTheme() {
            const currentTheme = document.documentElement.getAttribute('data-theme');
            const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
            document.documentElement.setAttribute('data-theme', newTheme);
            
            // 更新按钮文本和图标
            const themeToggle = document.getElementById('themeToggle');
            if (newTheme === 'dark') {
                themeToggle.innerHTML = '<span class="theme-icon">🌙</span> 暗黑主题';
            } else {
                themeToggle.innerHTML = '<span class="theme-icon">☀️</span> 明亮主题';
            }
        }
        
        // 获取设备列表
        function loadDevices() {
            $.get('/api/devices')
                .done(function(data) {
                    let html = '';
                    if (data.length === 0) {
                        html = '<p class="text-center">暂无设备</p>';
                    } else {
                        data.forEach(function(device) {
                            const statusClass = device.status === 'online' ? 'status-online' : 'status-offline';
                            const statusText = device.status === 'online' ? '在线' : '离线';
                            
                            html += `
                                <div class="card device-card" onclick="viewDeviceDetail('${device.id}')">
                                    <div class="device-card-body">
                                        <div class="device-card-header">${device.name}</div>
                                        <div>设备ID: ${device.id}</div>
                                        <div class="mt-2">
                                            <span class="device-status ${statusClass}">${statusText}</span>
                                        </div>
                                        <div class="mt-2">
                                            <button class="btn btn-sm btn-primary" onclick="event.stopPropagation(); controlDevice('${device.id}', 'start')">启动</button>
                                            <button class="btn btn-sm btn-warning" onclick="event.stopPropagation(); controlDevice('${device.id}', 'stop')">停止</button>
                                        </div>
                                    </div>
                                </div>
                            `;
                        });
                    }
                    $('#devicesGrid').html(html);
                })
                .fail(function() {
                    $('#devicesGrid').html('<p class="text-center text-danger">加载设备列表失败</p>');
                });
        }
        
        // 查看设备详情
        function viewDeviceDetail(deviceId) {
            window.location.href = `/device/${deviceId}`;
        }
        
        // 控制设备
        function controlDevice(deviceId, action) {
            const actionText = action === 'start' ? '启动' : '停止';
            if (confirm(`确定要${actionText}设备 ${deviceId} 吗？`)) {
                $.post(`/api/control/${deviceId}/${action}`)
                    .done(function() {
                        alert(`${actionText}设备成功`);
                        loadDevices(); // 刷新设备列表
                    })
                    .fail(function() {
                        alert(`${actionText}设备失败`);
                    });
            }
        }
        
        // 页面加载完成后初始化
        $(document).ready(function() {
            // 绑定主题切换按钮事件
            $('#themeToggle').click(toggleTheme);
            
            // 绑定刷新按钮事件
            $('#refreshDevices').click(function() {
                loadDevices();
            });
            
            // 首次加载数据
            loadDevices();
            
            // 设置定时刷新（30秒）
            setInterval(function() {
                loadDevices();
            }, 30000);
        });
    </script>
</body>
</html>